$(document).ready(function(){
	$("#uploadFileButton").click(function(event){
		$.ajaxFileUpload({
			url:'photoupload',
			secureuri:false,
			fileElementId:"uploadPhotoInput",
			dataType: 'json',
			success:function(data, status){
				try{
					$("#registerPhotoImg").attr("src", "photo/" + data.successMessage);
					$("#photoIdInput").val(data.successMessage);
				}catch(ex){
					alert(ex);
				}
			}
		});
	});
	$("#submitButton").click(function(){
		if($("#emailInput").val().search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)==-1){
			alert("Please input a correct Email.");
			$("#emailInput")[0].focus();
			return false;
		}
		if($("#passwordInput").val() == ""){
			alert("Please input the password.");
			$("#passwordInput")[0].focus();
			return false;
		}
		if($("#passwordInput").val() != $("#passwordConfirmInput").val()){
			alert("The two passwords you entered did not match.");
			$("#passwordInput")[0].focus();
			return false;
		}
		$.ajaxFileUpload({
			url:'photoupload',
			secureuri:false,
			fileElementId:"uploadPhotoInput",
			dataType: 'json',
			success:function(data, status){
				if(data.successMessage != ""){
					try{
						$("#registerPhotoImg").attr("src", "photo/" + data.successMessage);
						$("#photoIdInput").val(data.successMessage);
					}catch(ex){
						alert(ex);
					}
				}
				$.post("hf/register", {
					name:$("#nameInput").val(),
					email:$("#emailInput").val(),
					password:$("#passwordInput").val(),
					photoId:$("#photoIdInput").val()
				}, function(data){
					if(data.success){
						window.location.href="hfHome";
					}else{
						alert(data.errorMessage);
					}
				}, "json");
			}
		});
	});
});